<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class CaseCase extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'case_cases';
    protected $fillable = ['admin_user_id'];
    public function caseProgress()
    {
        return $this->hasMany(CaseCaseProgress::class);
    }
    public function caseEvents()
    {
        return $this->hasMany(CaseEvent::class);
    }

    public function adminUser()
    {
        return $this->belongsTo(AdminUser::class);
    }

    public function caseBatch()
    {
        return $this->belongsTo(CaseBatch::class);
    }
    public function caseReceipts()
    {
        return $this->hasMany(CaseReceipt::class);
    }

    public function attachments()
    {
        return $this->hasMany(Attachment::class);
    }

        /**
     * 获取实时支出
     */
    public function getCalcSalesExpensesAttribute()
    {
        $calcSalesExpenses= 0;
        foreach($this->caseReceipts as $receipt)
        {
            if ($receipt->type === 2){
                $calcSalesExpenses+=$receipt->receive;
            }
        }
        return $calcSalesExpenses;
    }

        /**
     * 获取实时收款
     */
    public function getCalcSalesRevenueAttribute()
    {
        $calcSalesRevenue= 0;
        foreach($this->caseReceipts as $receipt)
        {
            if ($receipt->type === 1){
                $calcSalesRevenue+=$receipt->receive;
            }
        }
        return $calcSalesRevenue;
    }
}
